package main

import (
	"fmt"
	"github.com/gotk3/gotk3/gdk"
	"github.com/gotk3/gotk3/glib"
	"github.com/gotk3/gotk3/gtk"
	"log"
)

// IDs to access the tree view columns by
const (
	COLUMN_VERSION = iota
	COLUMN_FEATURE
)

var win *gtk.Window
var menu *gtk.Menu
var tview *gtk.TreeView
func elog(err error){if err != nil {log.Fatal("Could not create menu:", err)}}

func buildMenu() *gtk.Menu {
	menu, err := gtk.MenuNew()                             ;elog(err)
	item, err := gtk.MenuItemNewWithLabel("Quit")    ;elog(err)
	item.Show()
	item.Connect("activate", func() {win.Close()})

	menu.Append(item)

	item, err = gtk.MenuItemNewWithLabel("Tree view")    ;elog(err)
	item.Show()
	item.Connect("activate", func() {
		//window.Present()
		win.SetTitle("点击菜单了")
		selection,err:= tview.GetSelection()   ;elog(err)
		tvmodel,err :=tview.GetModel()

		var iter *gtk.TreeIter
		var model gtk.ITreeModel
		//var store *gtk.ListStore
		var ok bool
		//store = (*gtk.ListStore)(tvmodel)
		model, iter, ok = selection.GetSelected()
		if ok {
			tpath, err := model.(*gtk.TreeModel).GetPath(iter)
			v1,err:=tvmodel.GetValue(iter,0) ;elog(err)
			v2,err:=tvmodel.GetValue(iter,1) ;elog(err)
			col1 ,err :=v1.GetString()
			col2 ,err :=v2.GetString()
			log.Printf("selected path: %s,%s,%s\n", tpath,col1,col2)
		}
		//

		/*nenf:=tvmodel.IterNChildren(iter)
		var child gtk.TextIter
		for ind:=0;ind<nenf;ind++{
			tvmodel.IterNthChild(&child,iter,ind)

		}*/

	})

	menu.Append(item)

	return menu
}
// Add a column to the tree view (during the initialization of the tree view)
func createColumn(title string, id int) *gtk.TreeViewColumn {
	cellRenderer, err := gtk.CellRendererTextNew()
	if err != nil {
		log.Fatal("Unable to create text cell renderer:", err)
	}

	column, err := gtk.TreeViewColumnNewWithAttribute(title, cellRenderer, "text", id)
	if err != nil {
		log.Fatal("Unable to create cell column:", err)
	}

	return column
}

// Creates a tree view and the list store that holds its data
func setupTreeView() (*gtk.TreeView, *gtk.ListStore) {
	treeView, err := gtk.TreeViewNew()
	if err != nil {
		log.Fatal("Unable to create tree view:", err)
	}

	treeView.AppendColumn(createColumn("Version", COLUMN_VERSION))// 0 列
	treeView.AppendColumn(createColumn("Feature", COLUMN_FEATURE))// 1 列

	// Creating a list store. This is what holds the data that will be shown on our tree view.
	listStore, err := gtk.ListStoreNew(glib.TYPE_STRING, glib.TYPE_STRING)
	if err != nil {
		log.Fatal("Unable to create list store:", err)
	}
	treeView.SetModel(listStore)

	return treeView, listStore
}

// Append a row to the list store for the tree view
func addRow(listStore *gtk.ListStore, version, feature string) {
	// Get an iterator for a new row at the end of the list store
	iter := listStore.Append()

	// Set the contents of the list store row that the iterator represents
	err := listStore.Set(iter,
		[]int{COLUMN_VERSION, COLUMN_FEATURE},
		[]interface{}{version, feature})

	if err != nil {
		log.Fatal("Unable to add row:", err)
	}
}

// Create and initialize the window
func setupWindow(title string) *gtk.Window {
	win, err := gtk.WindowNew(gtk.WINDOW_TOPLEVEL)
	if err != nil {
		log.Fatal("Unable to create window:", err)
	}

	win.SetTitle(title)
	win.Connect("destroy", func() {
		gtk.MainQuit()
	})
	win.SetPosition(gtk.WIN_POS_CENTER)
	win.SetDefaultSize(600, 300)
	return win
}

func main() {
	gtk.Init(nil)

	win = setupWindow("Go Feature Timeline")

	treeView, listStore := setupTreeView()
	tview = treeView
	win.Add(treeView)

	// Add some rows to the list store
	addRow(listStore, "第一句", "故人西辞黄鹤楼")
	addRow(listStore, "第二句", "烟花三月下扬州")
	addRow(listStore, "第三句", "孤帆远影碧空尽")
	addRow(listStore, "第四句", "唯见长江天际流")
	addRow(listStore, "作者", "唐·李白")
	addRow(listStore, "诗名", "黄鹤楼送孟浩然之广陵 ")

	menu = buildMenu()
	treeView.Connect("button_press_event", tvpressed,menu)

	win.ShowAll()
	gtk.Main()
}
func tvpressed(tv *gtk.TreeView,evt *gdk.Event,menu *gtk.Menu)  {
	//var kbn uint64
	//kbn = *(*uint64)(unsafe.Pointer(evt.GdkEvent))
	event:=gdk.EventButtonNewFromEvent(evt)
	if event.Type()  == gdk.EVENT_BUTTON_PRESS { //单击
		fmt.Printf("单击, ")
	} else if event.Type() == gdk.EVENT_DOUBLE_BUTTON_PRESS { //双击
		fmt.Printf("双击, ")
	}
	fmt.Println("button = ", event.Button())
	if event.Button() == 1 {
		fmt.Printf("左键, ")
	} else if event.Button() == 2 {
		fmt.Printf("中间键, ")
	} else if event.Button() == 3 {
		fmt.Printf("右键, ")
		menu.PopupAtPointer(evt)  //能在光标位置弹出菜单
	}

	fmt.Println("坐标:", event.X(), event.Y())

}